package service;
import util.RegexUtil;
import dao.UserDAO;
import domain.*;
import ex.*;


public class UserService {

	ActorService actorService=new ActorService();
	UserDAO userDao=new UserDAO();
	public User authenticateUser(String email, String password) throws DatabaseException, AuthenticationFailedException
	{		
		User user= userDao.selectUserByEmailAndPassword(email, password);
		if(user==null){
			throw new AuthenticationFailedException();
		}
		else{
			return user;
		}
	}
	public User getUserByUid(long uid) throws DatabaseException{
		return userDao.selectUserByUid(uid);
	}
	public User getUserByEmail(String email) throws DatabaseException{
		return userDao.selectUserByEmail(email);
	}
	public boolean isAlreadyPresentEmail(String email) throws DatabaseException{
		return userDao.checkUserByEmail(email);
	}
	public void createUser(User user, String password2) throws RegistrationFailedException, DatabaseException{
		if(user!=null){
			if(RegexUtil.isNullOrEmpty(user.getFirstName())){
				throw new RegistrationFailedException("You must enter first name!");
			}
			if(RegexUtil.isNullOrEmpty(user.getLastName())){
				throw new RegistrationFailedException("You must enter surname!");
			}
			if(RegexUtil.isNullOrEmpty(user.getEmail())){
				throw new RegistrationFailedException("You must enter email!");
			}
			else{
				if(!RegexUtil.isValidEmail(user.getEmail())){
					throw new RegistrationFailedException("Email is not valid!");
				}
				else{
					String email=user.getEmail();
					if(isAlreadyPresentEmail(email))
						throw new RegistrationFailedException("Email already registered!");
				}
			}
			if(RegexUtil.isNullOrEmpty(user.getPassword())){
				throw new RegistrationFailedException("You must enter password!");
			}
			else{
				if(user.getPassword().length()<8 ||user.getPassword().length()>16)
					throw new RegistrationFailedException("Password lenght must be at least 8 and at most 16!");
				else if(!user.getPassword().equals(password2)){
					throw new RegistrationFailedException("Passwords not match!");
				}
			}
			if(!RegexUtil.isNullOrEmpty(user.getTel())){
				if(!RegexUtil.isParsableToInt(user.getTel())){
					throw new RegistrationFailedException("Your tel no must consist of digits!");
				}
			}
			else{
				user.setTel("-1");
			}
			if(RegexUtil.isNullOrEmpty(user.getCity())){
				user.setCity("");
			}
			int title=user.getTitle();
			//success
			userDao.insertUser(user);
			
			if(user.getActorFeatures()!=null){
				try{
					User us=getUserByEmail(user.getEmail());
					Actor actor=user.getActorFeatures();
					actor.setUid(us.getUid());
					actorService.createActor(user.getActorFeatures());
				}
				catch (Exception e) {
					title=(title>>1)<<1;
					user.setTitle(title);
					userDao.editUser(user);
					throw new RegistrationFailedException("Registration succeded but actor definition is not. Please add this title later on! ("+e.getMessage()+")");
				}
			}			
		}
		else{
			throw new RegistrationFailedException("Registration failed!");
		}		
	}
	public void updateUser(User user) throws RegistrationFailedException, DatabaseException
	{
		if(user!=null){
			if(RegexUtil.isNullOrEmpty(user.getFirstName())){
				throw new RegistrationFailedException("You must enter first name!");
			}
			if(RegexUtil.isNullOrEmpty(user.getLastName())){
				throw new RegistrationFailedException("You must enter surname!");
			}
			if(!RegexUtil.isNullOrEmpty(user.getTel())){
				if(!RegexUtil.isParsableToInt(user.getTel())){
					throw new RegistrationFailedException("Your tel no must consist of digits!");
				}
			}
			else{
				user.setTel("-1");
			}
			if(RegexUtil.isNullOrEmpty(user.getCity())){
				user.setCity("");
			}
			int title=user.getTitle();
			//success
			userDao.editUser(user);
			
			if(user.getActorFeatures()!=null){
				User us=null;
				try{
					us=getUserByEmail(user.getEmail());
				}
				catch (Exception e) {							

					throw new RegistrationFailedException("Update succeded but actor definition is not. Please add this title later on! ("+e.getMessage()+")");
				}
				
				Actor actor=user.getActorFeatures();
				if(actor.getUid()==user.getUid()){
					actorService.updateActor(actor);
				}
				else{
					try{
						actor.setUid(us.getUid());
						actorService.createActor(user.getActorFeatures());
					}
					catch (Exception e) {							
						title=(title>>1)<<1;
						user.setTitle(title);
						userDao.editUser(user);
						throw new RegistrationFailedException("Update succeded but actor definition is not. Please add this title later on! ("+e.getMessage()+")");
					}
				}				
			}			
		}
		else{
			throw new RegistrationFailedException("Update failed!");
		}				
	}
	
}
